package gui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;

import javax.swing.*;

import bootLoader.FakeLoader;

import components.exceptions.IntToBinaryException;
import components.register.RegisterGroup;

public class PowerButton extends JButton {
	public int timesOfClicked;
	public ImageIcon icon1, icon2;
	public static boolean isOn;

	public PowerButton() {
		timesOfClicked = 0;
	}

	public PowerButton(String s1, String s2) {
		timesOfClicked = 0;
		isOn = false;

		URL url1 = TestMain.class.getResource(TestMain.imgDir + s1);
		URL url2 = TestMain.class.getResource(TestMain.imgDir + s2);

		icon1 = new ImageIcon(url1);
		icon2 = new ImageIcon(url2);

		// icon3 = new ImageIcon("Images/"+s3);
		this.setIcon(icon1);
		this.setContentAreaFilled(false);
		this.setBorder(null);
		this.setOpaque(false);

		this.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				// if(powerButton.on==true){
				int i = getTimesOfClicked();
				setTimesOfClicked(i + 1);
				if (getTimesOfClicked() % 2 == 1) {
					setIcon(icon2);
					isOn = true;

					// start loader:
					FakeLoader fl = new FakeLoader();
					try {
						fl.init();
						fl.start();
					} catch (IntToBinaryException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				} else {
					setIcon(icon1);
					isOn = false;
				}
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				// setIcon(icon3);
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
			}

		});
	}

	public int getTimesOfClicked() {
		return timesOfClicked;
	}

	public void setTimesOfClicked(int i) {
		timesOfClicked = i;
	}

	// public static boolean getOn() {
	// return on;
	// }
}
